package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.StringRes;

import com.facebook.common.util.UriUtil;
import com.hjq.base.BaseDialog;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.widget.SimpleAnimationDraweeView;

import java.io.IOException;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/12/2
 *    desc   : 等待加载对话框
 */
public final class WaitDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder> {

        private final TextView mMessageView;
        private GifImageView gifImageView;
        private GifDrawable gifDrawable;
//        private final SimpleAnimationDraweeView simpleAnimationDraweeView;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.wait_dialog);
            setAnimStyle(BaseDialog.ANIM_TOAST);
            setBackgroundDimEnabled(false);
            setCancelable(false);

            gifImageView = findViewById(R.id.gif_image_view);
//            simpleAnimationDraweeView = findViewById(R.id.loading_view);
            mMessageView = findViewById(R.id.tv_wait_message);
//            simpleAnimationDraweeView.setImageURI(UriUtil.getUriForResourceId(R.drawable.img_loading).toString());
//            simpleAnimationDraweeView.startAnimation();
            if (gifDrawable == null) {
                try {
                    int id = AppApplication.getInstance().getResourceId("img_loading", "drawable");
                    gifDrawable = new GifDrawable(getResources(), id);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            gifDrawable.reset();
            gifImageView.setImageDrawable(gifDrawable);
            gifDrawable.start();
            addOnDismissListener(new BaseDialog.OnDismissListener() {
                @Override
                public void onDismiss(BaseDialog dialog) {
                    gifDrawable.stop();
//                    simpleAnimationDraweeView.stopAnimation();
                }
            });
            addOnShowListener(new BaseDialog.OnShowListener() {
                @Override
                public void onShow(BaseDialog dialog) {
                    gifDrawable.start();
//                    simpleAnimationDraweeView.startAnimation();

                }
            });
        }

        public Builder setMessage(@StringRes int id) {
            return setMessage(getString(id));
        }

        public Builder setMessage(CharSequence text) {
            mMessageView.setText(text);
            mMessageView.setVisibility(text == null ? View.GONE : View.VISIBLE);
            return this;
        }
    }
}